﻿using System;
using System.Collections;
using System.Globalization;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common;
using Common.Entities;
using OrderSales.Interface;
using OrderSales.Presenter;

namespace OrderSales
{
    public partial class SelectGoodsForm : Form, ISelectGoodsView
    {

     
        private Dictionary<bool,string> textBut = new Dictionary<bool, string>(2);
        private bool GoFiltered = false;

        public SelectGoodsForm(Order order)
        {
            InitializeComponent();
            new SelectGoodsPresenter(this, order);
            textBut.Add(true, "Λ");
            textBut.Add(false, "V");
            goodsDataGrid.RowHeadersVisible = false;
            GoFiltered = true;
            button11.Text = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
            //goodsDataGridTableStyle.MappingName = groupComboBox.DataSource.GetType().ToString();
        }

        private void digitButton_Click(object sender, EventArgs e)
        {
            quantityTextBox.Text += ((Button) sender).Text;
        }

        private void otherButton_Click(object sender, EventArgs e)
        {
            otherButton.Text = textBut[otherPanel.Visible];
            otherPanel.Visible = !otherPanel.Visible;
        }


        private void goodsDataGrid_MouseDown(object sender, MouseEventArgs e)
        {
            DataGrid.HitTestInfo hti = goodsDataGrid.HitTest(e.X, e.Y);
            //if (hti.Type == DataGrid.HitTestType.ColumnHeader)
            //{
            //    int col = hti.Column;
            //    s.Add(goodsDataGridTableStyle.GridColumnStyles[col].MappingName);
            //    goodsDataGridTableStyle.GridColumnStyles[col].HeaderText = headers[col] + s.GetLogo(goodsDataGridTableStyle.GridColumnStyles[col].MappingName);
            //    allGoodsBindingSource.Sort = s.FilterString();
            //}
            if (hti.Type == DataGrid.HitTestType.Cell)
            {
                if (!otherPanel.Visible)
                {
                    otherButton_Click(null, null);
                }
            }
            else
                if (otherPanel.Visible)
                {
                    otherButton_Click(null, null);
                }


        }

        private void groupComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (GoFiltered)
            {
                GoFiltered = false;
                filterGroupButton.Tag = "NoFilter";
                filterGroupButton_Click(null, null);
                ChangeFilter(null, null);
                GoFiltered = true;
            }

        }


        private void markComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (GoFiltered)
            {
                GoFiltered = false;
                filterMarkButton.Tag = "NoFilter";
                filterMarkButton_Click(null, null);
                ChangeFilter(null, null);
                GoFiltered = true;
            }
        }

        private void filterGroupButton_Click(object sender, EventArgs e)
        {
            if ((string) filterGroupButton.Tag == "Filter")
            {
                filterGroupButton.Tag = "NoFilter";
                filterGroupButton.BackColor = SystemColors.Window;
            }
            else
            {
                filterGroupButton.Tag = "Filter";
                filterGroupButton.BackColor = SystemColors.Control;
            }
            if (GoFiltered)
            {
                GoFiltered = false;
                ChangeFilter(null, null);
                GoFiltered = true;
            }
        }

        private void filterMarkButton_Click(object sender, EventArgs e)
        {
            if ((string) filterMarkButton.Tag == "Filter")
            {
                filterMarkButton.Tag = "NoFilter";
                filterMarkButton.BackColor = SystemColors.Window;
            }
            else
            {
                filterMarkButton.Tag = "Filter";
                filterMarkButton.BackColor = SystemColors.Control;

            }
            if (GoFiltered)
            {
                GoFiltered = false;
                ChangeFilter(null, null);
                GoFiltered = true;
            }
        }


        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (otherPanel.Visible)
            {
                otherButton_Click(null, null);
            }

        }

        private void addButton_Click(object sender, EventArgs e)
        {
            try
            {
                AddRecordOrder(null, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region ISelectGoodsView Members

        public List<string> ListGroups
        {
            set { groupComboBox.DataSource = value; }
        }

        public List<string> ListMarks
        {
            set { markComboBox.DataSource = value; }
        }

        public List<Good> ListGoods
        {
            set { 
                goodsDataGrid.DataSource = value; 
                goodsDataGridTableStyle.MappingName = goodsDataGrid.DataSource.GetType().Name; 
            }
        }

        public string FilterGroup
        {
            get { return groupComboBox.Text; }
        }

        public string FilterMark
        {
            get { return markComboBox.Text; }
        }

        public bool IsFilterGoup
        {
            get { return (string) filterGroupButton.Tag == "Filter"; }
        }

        public bool IsFilterMark
        {
            get { return (string) filterMarkButton.Tag == "Filter"; }
        }

        public int SelectGoodIndex
        {
            get
            {
                return goodsDataGrid.CurrentRowIndex;
            }
        }

        public double Quantity
        {
            get { return Double.Parse(quantityTextBox.Text); }
        }

        public int Price
        {
            get
            {
                return int.Parse(priceButton.Text);
            }
        }

        public event EventHandler<EventArgs> ChangeFilter;

        public event EventHandler<EventArgs> AddRecordOrder;

        #endregion

        private void closeButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void goodsDataGrid_CurrentCellChanged(object sender, EventArgs e)
        {
            quantityTextBox.Text = "";
        }

        private void priceButton_Click(object sender, EventArgs e)
        {
            selectPricecontextMenu.Show((Control)sender, new Point(((Control)sender).Left, ((Control) sender).Top));
        }

        private void menuItem_Click(object sender, EventArgs e)
        {
            priceButton.Text = ((MenuItem)sender).Text;
            priceDataGridTextBoxColumn.MappingName = "Price" + ((MenuItem)sender).Text;
            goodsDataGrid.Refresh();

        }

        private void quantityTextBox_TextChanged(object sender, EventArgs e)
        {
            if (quantityTextBox.Text != "")
            {
                amountLabel.Text = (double.Parse(quantityTextBox.Text) * double.Parse(goodsDataGrid[
                                                                                       goodsDataGrid.CurrentRowIndex,
                                                                                       goodsDataGridTableStyle.
                                                                                           GridColumnStyles.IndexOf(
                                                                                           priceDataGridTextBoxColumn)].
                                                                                       ToString())).ToString();
                addButton.Enabled = true;
                backButton.Enabled = true;
            }
            else
            {
                amountLabel.Text = "0.00";
                addButton.Enabled = false;
                backButton.Enabled = false;
            }
        }

        private void backButton_Click(object sender, EventArgs e)
        {
            quantityTextBox.Text.Remove(quantityTextBox.Text.Length - 2, 1);
        }
    }
}